#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 100005;
int p[MAXN];

struct brick{
	int m, d;
}b[MAXN];

bool cmp(brick x, brick y){
	return x.d <= y.d;
}

int main(){
	int t, N, T;
	scanf("%d", &t);
	while(t--){
		scanf("%d", &N);
		for(int i=0;i<N;i++){
			scanf("%d %d", &b[i].m, &b[i].d);
		}
		scanf("%d", &T);
		for(int i=0;i<T;i++){
			scanf("%d", &p[i]);
		}
		
//		sort(p, p+T);
		sort(b, b+N, cmp);
		
		int score = 0, F = 1, t = 0, cntp = 0;
		for(int i=0;b[N-1].m>0;){
			score += F * b[i].d;
			b[i].m--;
			if(b[i].m == 0){
				i++;
			}
			t++;
			if(t == p[cntp]){
				F++;
				cntp++;
			}
		}
		printf("%d\n", score);
	}
	return 0;
}
